package com.uu.it.tracker.sensordata;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import android.util.Log;

public class SensorInformer {
	
	/**
	 * Sensor accessing interface
	 * */
	private static final String SENSORFILENAME = "/dev/ttyUSB0";
	
	/**
	 * Command to sensor to start session
	 * */
	private static final String startCommand = "start\n";
	
	/**
	 * Command to sensor to end session
	 * */
	private static final String endCommand = "close\n";
	
	/**
	 * send command to sensor
	 * */
	private void informSensor(String command) {
		File sensorFile = new File(SENSORFILENAME);
	    try {
	    	FileOutputStream os = new FileOutputStream(sensorFile);
			BufferedWriter output = new BufferedWriter(new OutputStreamWriter(os));
			output.write(command);
		    Log.i("sensorInformer", command);
			output.flush();
	    	output.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.i("FileNotFound", e.toString());
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			Log.i("UnsupportedEncodingException", e.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.i("IOException", e.toString());
		}
	}
	
	/**
	 * start the session
	 * */
	public void startSession() {
		informSensor(startCommand);
	}
	
	/**
	 * end the session
	 * */
	public void endSession() {
		informSensor(endCommand);
	}
	
}